<template>
    <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
        <div class="site-navbar__header">
            <h1 class="site-navbar__brand" @click="$router.push({name: 'home'})">
                <a class="site-navbar__brand-lg" href="javascript:">矿校云</a>
                <a class="site-navbar__brand-mini" href="javascript:">矿校云</a>
            </h1>
        </div>
        <div class="site-navbar__body clearfix">
            <!--<el-menu class="site-navbar__menu" mode="horizontal">
                      <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
                          <icon-svg name="zhedie"/>
                      </el-menu-item>
            </el-menu>-->
            <el-menu
                    class="site-navbar__menu"
                    mode="horizontal"
                    :default-active="$route.path.split('/')[1]"
            >
                <!-- <el-menu-item index="home" @click="$router.push({name: 'home'})">
                    <span slot="title">首页</span>
                </el-menu-item> -->
                <el-menu-item
                        v-for="menu in menuList"
                        :index="menu.menuId + ''"
                        :key="menu.menuId + ''"
                        @click="gotoRouteHandle(menu)"
                >
                    <!--<icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>-->
                    <span>{{ menu.name }}</span>
                </el-menu-item>
            </el-menu>
            <el-menu class="site-navbar__menu site-navbar__menu--right" mode="horizontal">
                <el-menu-item index="2" @click="noticeHandle()" v-if="isAuth('user:certmanage:update')">
                    <template slot="title">
                        <el-badge v-model="alertListCount" v-if="alertListCount > 0">
                            <i class="el-icon-bell"></i>
                        </el-badge>
                        <i v-else class="el-icon-bell"></i>
                    </template>
                </el-menu-item>
                <!--<el-menu-item index="1" @click="$router.push({ name: 'theme' })">
                            <template slot="title">
                                <el-badge value="new">
                                    <span><icon-svg name="shezhi" class="el-icon-setting"/></span>
                                </el-badge>
                            </template>
                </el-menu-item>-->
                <el-menu-item class="site-navbar__avatar" index="3">
                    <el-dropdown trigger="click">
            <span
                    class="el-dropdown-link"
                    style="display:inline-block;min-width: 50px;text-align: center"
            >
              <!--<img src="~@/assets/img/avatar.png" :alt="userName">-->
              {{ userName }}
            </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>
                            <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </el-menu-item>
            </el-menu>
        </div>
        <!-- 弹窗, 修改密码 -->
        <update-password v-if="updatePassowrdVisible" ref="updatePassowrd"/>
        <notice v-if="noticeVisible" :data="data" ref="notice"/>
    </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import notice from './notice'
  import {clearLoginInfo} from '@/utils'
  import {mapState} from 'vuex'

  export default {
    data () {
      return {
        val: null,
        data: '',
        updatePassowrdVisible: false,
        noticeVisible: false,
        list: [],
        dynamicMenuRoutes: []
      }
    },
    created () {
      this.dynamicMenuRoutes = JSON.parse(
        sessionStorage.getItem('dynamicMenuRoutes') || '[]'
      )
    },
    mounted () {
      this.isHome()
      this.$store.dispatch('user/loadAlertListCount')
      // todo 消息推送有问题
      // this.$http({
      //   url: this.$http.adornUrl('/usercenter-admin/user/certmanage'),
      //   method: 'post',
      //   data: {
      //     'page': this.pageIndex,
      //     'limit': this.pageSize
      //   }
      // }).then(res => {
      //   this.val = res.data ? res.data.totalCount : ''
      //   this.data = res.data
      // })
    },
    components: {
      UpdatePassword,
      notice
    },
    computed: {
      alertListCount: {
        get () {
          return this.$store.dispatch('user/loadAlertListCount')
        }
      },
      menuList: {
        get () {
          return this.$store.state.common.menuList
        },
        set (val) {
          this.$store.commit('common/updateMenuList', val)
        }
      },
      navbarLayoutType: {
        get () {
          return this.$store.state.common.navbarLayoutType
        }
      },
      /*
        * sidebarFold: {
          get () {
            return this.$store.state.common.sidebarFold
          },
          set (val) {
            this.$store.commit('common/updateSidebarFold', val)
          }
        },
        * */
      mainTabs: {
        get () {
          return this.$store.state.common.mainTabs
        },
        set (val) {
          this.$store.commit('common/updateMainTabs', val)
        }
      },
      ...mapState({
        alertListCount: state => state.user.alertListCount,
        userName: state => state.user.name
      })
    },
    methods: {
      // 修改密码
      updatePasswordHandle () {
        this.updatePassowrdVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassowrd.init()
        })
      },
      // 消息通知
      noticeHandle () {
        this.noticeVisible = true
        this.$nextTick(() => {
          this.$refs.notice.init()
        })
      },
      gotoRouteHandle (menu) {
        var route = this.dynamicMenuRoutes.filter(
          item => item.meta.menuId === menu.menuId
        )
        if (route.length >= 1) {
          this.$router.push(route[0].path)
        }
      },
      // 退出
      logoutHandle () {
        this.$confirm(`确定进行[退出]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl('/auth/logout'),
              method: 'post',
              data: {token: this.$cookie.get('token')}
            }).then(data => {
              if (data && data.code === 200) {
                clearLoginInfo()
                this.$router.push({name: 'login'})
              }
            })
          })
          .catch(() => {
          })
      },
      isHome (route) {
        // console.log(route)
        // this.$nextTick(() => {
        let _route = route || this.$route
        if (_route.path.split('/')[1] === 'home') {
          this.$store.commit('common/updateSidebarFold', true)
        } else {
          this.$store.commit('common/updateSidebarFold', false)
        }
        // })
      }
    },
    watch: {
      $route (to) {
        this.isHome(to)
      }
    }
  }
</script>

<style>
    .site-navbar {
        overflow: hidden;
    }
</style>
